package com.ecoolex.hotel.saas.core.manager;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.hotel.saas.core.dao.PresaleCouponCategoryDAO;
import com.ecoolex.hotel.saas.entity.PresaleCouponCategory;
import com.ecoolex.hotel.saas.entity.PresaleCouponCategory.Column;
import com.ecoolex.hotel.saas.entity.PresaleCouponCategoryExample;
import com.ecoolex.hotel.saas.entity.PresaleCouponCategoryExample.Criteria;

import java.time.Instant;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PresaleCouponCategoryManager {

    @Autowired
    private PresaleCouponCategoryDAO dao;

    @Autowired
    private PresaleCouponCategoryJointManager jointManager;

    public PresaleCouponCategory getPresaleCouponCategory(Integer categoryId) {
        return dao.selectByPrimaryKeyWithLogicalDelete(categoryId, PresaleCouponCategory.NOT_DELETED);
    }

    public Pagination<PresaleCouponCategory> queryPresaleCouponCategory(Integer storeId, String name, Page page) {
        PresaleCouponCategoryExample example = new PresaleCouponCategoryExample();
        Criteria criteria = example.createCriteria();
        if (Check.notNull(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andStoreIdEqualTo(storeId).andDeletedEqualTo(PresaleCouponCategory.NOT_DELETED);
        example.setOffset(page.getFirstIndex());
        example.setRows(page.getPageSize());
        example.orderBy(Column.weight.desc());
        return Pagination.create(page,
                dao.countByExample(example),
                () -> dao.selectByExample(example));
    }

    public Pagination<PresaleCouponCategory> queryForUser(Integer storeId, String name, Page page) {
        return Pagination.create(page, dao.count(storeId, name), () -> dao.query(storeId, name, page));
    }

    public PresaleCouponCategory addPresaleCouponCategory(PresaleCouponCategory add, String createUser) {
        add.setDeleted(PresaleCouponCategory.NOT_DELETED);
        add.setCreateUser(createUser);
        add.setCreateTime(Date.from(Instant.now()));
        add.setUpdateUser(createUser);
        add.setUpdateTime(Date.from(Instant.now()));
        dao.insert(add);
        return add;
    }

    public PresaleCouponCategory editPresaleCouponCategory(PresaleCouponCategory edit, String updateUser) {
        edit.setUpdateUser(updateUser);
        edit.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(edit, Column.excludes(Column.deleted, Column.storeId, Column.createUser, Column.createTime));
        return edit;
    }

    public Boolean delPresaleCouponCategory(PresaleCouponCategory record, String updateUser) {
        record.setDeleted(PresaleCouponCategory.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, Column.deleted, Column.updateUser, Column.updateTime);
        //删除关联预售券表
        jointManager.deleteByCategoryId(record.getCategoryId(), updateUser);
        return true;
    }
}